package com.soulkun.smarthome.ui.main.home.adapter

import android.content.Context
import androidx.databinding.ObservableArrayList
import androidx.databinding.ViewDataBinding
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppRecycleItemMainHomeDeviceRecentDataListBinding
import com.soulkun.smarthome.logic.dto.DeviceDataDTO
import soulkun.library.mvvm.adapter.BaseDataBindingRecyclerViewAdapter

class AppMainHomeDeviceRecentDataListAdapter(
    context: Context,
    dataList: ObservableArrayList<DeviceDataDTO>
) : BaseDataBindingRecyclerViewAdapter<DeviceDataDTO>(context, dataList) {

    override fun getContentLayoutResId(): Int {
        return R.layout.app_recycle_item_main_home_device_recent_data_list
    }

    override fun setContentViewHolderCreateListener(binding: ViewDataBinding) {

    }

    override fun setContentViewHolderBindListener(binding: ViewDataBinding, dataPosition: Int) {
        val data = dataList[dataPosition]
        (binding as AppRecycleItemMainHomeDeviceRecentDataListBinding).deviceDataDTO = data
        binding.clMainHomeDeviceRecentDataListContainerData.setBackgroundResource(
            when (dataPosition % 5) {
                0 -> {
                    R.drawable.app_shape_rectangle_c4_8dp_background_orange_00
                }
                1 -> {
                    R.drawable.app_shape_rectangle_c4_8dp_background_green_a9
                }
                2 -> {
                    R.drawable.app_shape_rectangle_c4_8dp_background_red_5c
                }
                3 -> {
                    R.drawable.app_shape_rectangle_c4_8dp_background_blue_c6
                }
                else -> {
                    R.drawable.app_shape_rectangle_c4_8dp_background_purple_ca
                }
            }
        )
        binding.tvMainHomeDeviceRecentDataListTextValue.text =
            if (data.value != null) {
                if (dataList[dataPosition].unit == "布尔值") {
                    dataList[dataPosition].unitSymbol.split("/").run {
                        if (dataList[dataPosition].value == "0") {
                            this[0]
                        }else{
                            this[1]
                        }
                    }
                }else{
                    dataList[dataPosition].value + dataList[dataPosition].unitSymbol
                }
            }else{
                "暂无数据"
            }
    }

}